<!DOCTYPE html>
<html>

<head>
    <title>自定义事件</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>{{ total }}</p>
        <counter-button v-on:increment="count" v-on:click.native="clicked"></counter-button>
        <counter-button v-on:increment="count"></counter-button>
    </div>
    <script type="text/javascript">
    Vue.component('counter-button', {
        template: '<button v-on:click="increment">{{ counter }}</button>',
        data: function() {
            return {
                counter: 0
            }
        },
        methods: {
            increment: function() {
                this.counter += 1
                this.$emit('increment', this.counter)
            }
        }
    })
    new Vue({
        el: '#app',
        data: {
            total: 0
        },
        methods: {
            /**
             * $emit( event, […args] )附加参数都会传给监听器回调
             */
            count: function(v) {
                this.total += 1
            },
            clicked: function() {
                console.log('clicked')
            }
        }
    })
    </script>
</body>

</html>
